<!-- 数据类别配置 -->
<template>
  <BaseMainSlot :show-search-box="false">
    <template v-slot:btn-box>
      <ActionBar :page-no="171" :has-edit="true" :add-active="addActive" :del-active="delActive" :edit-active="editActive"
        @add="add" @del="del" @edit="edit" />
    </template>
    <template v-slot:table-box>
      <div class="zhtop">
        <img v-if="homeBaseImg" :src="homeBaseImg" alt="">
      </div>
      <div class="zhbottom">
        <div class="zhbottom-left">
          <div class="bot-title">已展示数据类型</div>
          <div class="bot-main">
            <div v-for="(item, index) in hasitemtypelist" :key="index" class="bot-item" 
              @click="clickhasbutton(item,index)" :class="{isShowClick:isShowClick==index}">
              {{item.dataTypeName }}
            </div>
          </div>
        </div>
        <div class="zhbottom-right">
          <div class="bot-title">未展示数据类型</div>
          <div class="bot-main">
            <div v-for="(item, index) in nohasitemtypelist" :key="index" class="bot-item">{{ item.dataTypeName }}</div>
          </div>
        </div>
      </div>
    </template>
    <template v-slot:dialog>
      <el-dialog :close-on-click-modal='false' :title="nowChanceStau === 'add' ? '数据类别配置' : '数据类别修改'" :visible.sync="datatypedialogVisible" width="30%"
        :rules="rules" @close="clickcancel">
        <el-form ref="ruleForm" label-position="left" :model="formData" :rules="rules" class="el-form">
          <el-form-item label="选择数据类别 :" class="w50p" prop="dataTypeName">
            <el-select filterable clearable v-model="formData.dataType" value-key="dataTypeId" clearable :disabled="nowChanceStau !== 'add'"
              placeholder="数据类别选择" @change="datatypechange">
              <el-option v-for="(item, index) in (nowChanceStau === 'add' ? nohasitemtypelist : hasitemtypelist)"
                :key="index" :label="item.dataTypeName" :value="item" />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="单位 :" class="w50p" prop="unit">
            <el-select filterable clearable v-model="formData.unit" @change="unitchange" placeholder="请选择" allow-create filterable clearable>
              <el-option
                v-for="(item,index) in unitList"
                :label="item.unitName"
                :value="item.unitName"
                :key="index + 'unit'"
              />
            </el-select>
          </el-form-item> -->
          <el-form-item label="图标选择 :" class="w50p" prop="logoFileId">
            <uploadimage @child-event="getimageurl" />
          </el-form-item>
          <el-form-item label="位置选择 :" class="w50p" prop="xaxis">
            <div @click="innerVisible = true">
              <div v-show="!formData.xaxis" class="line-bottom">请选择位置</div>
              <div v-show="formData.xaxis" class="line-bottom">重新选择位置</div>
            </div>
          </el-form-item>
          <el-form-item v-if="nowChanceStau === 'edit'" label="配置状态 :" class="w50p" prop="configFlag">
            <el-switch v-model="formData.configFlag" active-text="已配置" inactive-text="未配置" @change="switchChange" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button class="cancelbtn" @click="clickcancel()">取 消</el-button>
          <el-button class="savebtn" type="primary" @click="save('ruleForm')">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog width="1342px" title="请拖动位置" :visible.sync="innerVisible" append-to-body class="innerVis"
        @close="innerVisible = false">
        <div class="pot-xy">
          <div class="homeBaseImg">
            <img v-if="homeBaseImg" :src="homeBaseImg" alt="">
          </div>
          <VueDragResize :x="formData.xaxis" :y="formData.yaxis" :w="300" :h="80" :is-draggable="true"
            :is-resizable="false" :parent-limitation="true" @dragstop="mczilongmet">
            <div class="mczilong">{{ nowselectdataTypeName }}</div>
          </VueDragResize>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button class="cancelbtn" @click="innerVisible = false">取 消</el-button>
          <el-button class="savebtn" type="primary" @click="innerVisible = false">确 定</el-button>
        </span>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>
<script>
import ActionBar from "@/components/ActionBar";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
import demoApi from "@/api/document";
import UnitManageApi from "@/api/basic-info-manage/unit-manage";
import DataTypeManageApi from "@/api/basic-info-manage/data-type-manage";
import uploadimage from "@/components/uploadimage/uploadimage.vue";
import { mymixin } from "@/utils/mixins";
import { getToken } from "@/utils/auth";
import VueDragResize from "vue-drag-resize";
export default {
  name: "DataTypeManage",
  components: { BaseMainSlot, ActionBar, VueDragResize, uploadimage },
  mixins: [mymixin],
  data () {
    return {
      isShowClick:null,
      // 弹框选择的数据名称
      nowselectdataTypeName: "数据类型",
      // 当前选中的id
      nowdataTypeConfigId: "",
      rules: {
        dataTypeName: [
          {
            required: true,
            message: "请选择数据",
            trigger: "blur"
          }
        ],
        logoFileId: [
          {
            required: true,
            message: "请上传图标文件",
            trigger: "blur"
          }
        ],
        xaxis: [
          {
            required: true,
            message: "请选择类别位置",
            trigger: "blur"
          }
        ],
        unit: [
          {
            required: true,
            message: "请选择单位",
            trigger: "blur"
          }
        ],
      },
      // 对话框状态
      datatypedialogVisible: false,
      innerVisible: false,
      // 已展示数据项类型
      hasitemtypelist: [],
      // 未展示数据项类型
      nohasitemtypelist: [],
      unitList: [],
      nohasitem: "",
      image: "",
      configFlag: 0,
      formData: {
        xaxis: 50,
        yaxis: 50,
      },
      homeBaseImg: ''
    };
  },
  watch: {
    nowChanceStau (newval) {
      if (newval === "add" || newval === "edit") {
        this.datatypedialogVisible = true;
      }
    }
  },
  mounted () {
    this.getTableDataList();
    this.queryUnit();
    this.formData.xaxis = 50;
    this.formData.yaxis = 50;
    this.homeBaseImg = getToken('homeBaseImg')
  },
  methods: {
    switchChange (e) {
      console.log(e);
    },
    unitchange (e) {
      console.log(this.formData);
    },
    queryUnit () {
      UnitManageApi.queryByCondition().then(res => {
        this.unitList = res.data;
      });
    },
    // 选择数据类型
    async datatypechange (e) {
      this.nowselectdataTypeName = e.dataTypeName;
      this.formData.dataType = { ...e, value: e.dataTypeId, label: e.dataTypeName };
      this.formData.dataTypeName = e.dataTypeName;
      // this.formData = {
      //   dataType: { ...e, value: e.dataTypeId, label: e.dataTypeName },
      //   dataTypeName: e.dataTypeName
      // };
    },
    // 更新数据类别定位
    mczilongmet (e) {
      this.formData["xaxis"] = e.left;
      this.formData["yaxis"] = e.top;
    },
    // ---上传图片相关方法------
    getimageurl (e) {
      let fd = new FormData();
      fd.append("file", e);
      demoApi.fileUpload(fd).then(res => {
        if (res.success) {
          this.formData.logoFileId = res.data;
          this.$message.success("上传成功");
        } else this.$message.error(res.message);
      });
    },
    // 点击已展示数据项，添加或修改
    async clickhasbutton (e,index) {
      let { data } = await DataTypeManageApi.queryById({ id: e.dataTypeId });
      this.nowdataTypeConfigId = e.dataTypeId;
      this.formData.dataTypeId = e.dataTypeId;
      this.formData.dataTypeName = e.dataTypeName;
      this.formData.logoFileId = e.logoFileId;
      this.$set(this.formData, 'configFlag', e.configFlag)
      this.$set(this.formData, 'unit', e.unit)
      this.formData.xaxis = parseFloat(data.xaxis) || 50;
      this.formData.yaxis = parseFloat(data.yaxis) || 50;
      this.$set(this.formData, "dataType", {
        ...e,
        value: data.dataTypeId,
        label: data.dataTypeName
      });
      this.nowselectdataTypeName = data.dataTypeName;

      this.editActive = true;
      // 删除的话就直接删除
      this.delActive = true;
      // 
      this.isShowClick=index
    },
    getTableDataList () {
      this.getnothasitemtype();
      this.gethasitemtype();
      this.resetActionBar()
    },
    // 获取已有数据项类型
    gethasitemtype () {
      DataTypeManageApi.queryExistence().then(res => {
        if (res.success) {
          this.hasitemtypelist = res.data;
          this.$message.success("获取已有数据项类型成功");
        } else {
          this.$message.error("获取已有数据项类型失败");
        }
        // console.log(res)
      });
    },
    // 获取未展示数据项类型
    getnothasitemtype () {
      DataTypeManageApi.queryNotExistence().then(res => {
        if (res.success) {
          this.nohasitemtypelist = res.data;
          this.$message.success("获取未展示数据项成功");
        } else {
          this.$message.error("获取未展示数据项失败");
        }
      });
    },
    del () {
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        
      })
        .then(() => {
          this.nowChanceStau = 'del'
          // this.delmessage()
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
          this.resetActionBar()
          if (this.tableData.length > 0) {
            this.$refs.tableData.clearSelection();
          } else if (this.treeData.length > 0) {
            this.$refs.tree.setCheckedKeys([]);
          }
        });
      this.formData = {}
      this.postName = ""
      this.nowChanceStau = "";
      this.canInput = true;
      this.addActive = true;
      this.saveActive = false;
      this.editActive = false
      this.cancelActive = false;
    },
    // 删除
    delmessage () {
      if (this.nowChanceStau === "del") {
        let delparam = this.nowdataTypeConfigId;
        DataTypeManageApi.delById({ id: delparam }).then(res => {
          if (res.success) {
            this.nowdataTypeConfigId = "";
            this.getTableDataList();
            this.resetActionBar()
            this.$message.success("删除成功");
          } else {
            this.resetActionBar()
            this.$message.error("删除失败");
          }
        });
      }
    },
    save (formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "add") {
            let addparams = {
              dataTypeId: this.formData.dataType.value,
              dataTypeName: this.formData.dataType.label,
              logoFileId: this.formData.logoFileId,
              xaxis: this.formData.xaxis,
              yaxis: this.formData.yaxis,
              unit: this.formData.unit,
              configFlag: true
            };
            DataTypeManageApi.edit(addparams).then(res => {
              if (res.success) {
                this.getTableDataList();
                this.$message.success("添加成功");
              } else if (res.errCode === 200) {
                this.$message.error(res.message);
              } else {
                this.$message.error("添加失败");
              }
              this.datatypedialogVisible = false;
              this.resetActionBar();
            });
          } else if (this.nowChanceStau === "edit") {
            let editparams = {
              dataTypeId: this.formData.dataType.value,
              dataTypeName: this.formData.dataType.label,
              logoFileId: this.formData.logoFileId,
              xaxis: this.formData.xaxis,
              yaxis: this.formData.yaxis,
              unit: this.formData.unit,
              configFlag: this.formData.configFlag
            };
            DataTypeManageApi.edit(editparams).then(res => {
              if (res.success) {
                this.getTableDataList();
                this.$message.success("修改成功");
              } else {
                this.$message.error("修改失败");
              }
              this.datatypedialogVisible = false;
              this.resetActionBar();
            });
          }
        } else {
          return false;
        }
      });
    },
    // 点击取消按钮重置状态
    clickcancel () {
      this.datatypedialogVisible = false;
      this.resetActionBar();
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .el-form-item {
  align-items: center !important;
}

.line-bottom {
  text-decoration: underline;
  letter-spacing: 1px;
  cursor: pointer;
}

.dia-line {
  width: 100%;
  display: flex;
  height: 36px;
  line-height: 36px;
  margin-bottom: 10px;

  .dia-title {
    width: 20%;
    white-space: nowrap;
  }

  .dia-content {
    width: 80%;
  }
}

.pot-xy {
  width: 1300px;
  max-width: 1300px;
  height: 600px;
  min-height: 600px;
  background-image: url("~@/assets/oth-img/fish-pond-background.png");
  background-repeat: no-repeat;
  background-position: top;
  background-size: 1300px 100%;
  position: relative;

  .homeBaseImg,
  .homeBaseImg>img {
    width: 100%;
    height: 100%;
  }

  .mczilong {
    height: 80px;
    line-height: 80px;
    background-color: #1fce4b;
    color: #fff;
    white-space: nowrap;
    text-align: center;
    cursor: pointer;
    font-weight: 600;
    font-size: 20px;
  }
}

.zhtop {
  width: 100%;
  height: 50%;
  // border: solid springgreen 2px;
  margin-bottom: 1%;
  background-image: url("~@/assets/oth-img/fish-pond-background.png");
  background-position: bottom;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  border-radius: 10px;

  img {
    height: 100%;
    width: 100%;
  }
}

.zhbottom {
  width: 100%;
  height: 47%;
  display: flex;
  justify-content: space-between;

  .zhbottom-left {
    width: 50%;
    height: 100%;
    border: 1px solid rgba(6, 191, 249, 1);

    .bot-title {
      width: 100%;
      height: 35px;
      font-size: 23px;
      line-height: 35px;
      color: #fff;
      background-color: rgba(6, 191, 249, 1);
      text-align: center;
      letter-spacing: 14px;
    }

    .bot-main {
      width: 100%;
      // height: calc(100% - 35px);
      max-height: calc(100% - 35px);
      padding: 10px;
      font-size: 20px;
      display: flex;
      flex-wrap: wrap;
      flex-direction: row;
      // justify-content: center;
      align-items: center;
      overflow: auto;

      .bot-item {
        border: 1px solid rgba(6, 191, 249, 1);
        min-width: 145px;
        height: 36px;
        text-align: center;
        line-height: 36px;
        border-radius: 6px;
        font-weight: 700;
        padding: 0px 10px;
        margin: 5px;
        cursor: pointer;
        &:hover{
          background: rgba(6, 191, 249, 1);
          color: rgba(0, 0, 0, 0.6);
        }
      }
      .isShowClick{
        background: rgba(6, 191, 249, 1);
        color: rgba(0, 0, 0, 0.6);
      }
    }
  }

  .zhbottom-right {
    width: 49%;
    height: 100%;
    border: 1px solid rgba(217, 217, 217, 1);

    .bot-title {
      width: 100%;
      height: 35px;
      font-size: 23px;
      line-height: 35px;
      background-color: #d9d9d9;
      color: rgba(1, 59, 77, 1);
      letter-spacing: 14px;
      font-size: 26px;
      text-align: center;
    }

    .bot-main {
      width: 100%;
      max-height: calc(100% - 35px);
      padding: 10px;
      font-size: 20px;
      display: flex;
      flex-wrap: wrap;
      flex-direction: row;
      align-items: center;
      overflow: auto;

      .bot-item {
        border: 2px solid #d9d9d9;
        min-width: 100px;
        height: 40px;
        padding: 0px 10px;

        text-align: center;
        line-height: 36px;
        border-radius: 5px;
        font-weight: 700;
        white-space: nowrap;
        margin: 5px;
      }
    }
  }
}
</style>
